package com.fanfan.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fanfan.constant.SystemConstants;
import com.fanfan.dao.TradeDao;
import com.fanfan.enums.AppHttpCodeEnum;
import com.fanfan.exception.SystemException;
import com.fanfan.pojo.Pet;
import com.fanfan.pojo.Trade;
import com.fanfan.pojo.TradeVo;
import com.fanfan.pojo.domain.ResponseResult;
import com.fanfan.pojo.vo.PageVo;
import com.fanfan.service.TradeService;
import com.fanfan.service.UserService;
import com.fanfan.utils.BeanCopyUtils;
import com.fanfan.utils.SecurityUtils;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * (Trade)表服务实现类
 *
 * @author fansea
 * @since 2023-03-11 18:02:51
 */
@Service("tradeService")
public class TradeServiceImpl extends ServiceImpl<TradeDao, Trade> implements TradeService {
    @Autowired
    UserService userService;

    @Override
    public ResponseResult tradeList(Integer pageNum, Integer pageSize) {
//        if (SecurityUtils.getUserId())
        LambdaQueryWrapper<Trade> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Trade::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.gt(Trade::getCounts,0);
        queryWrapper.orderByDesc(Trade::getIsTop);
        //分页查询
        Page<Trade> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        List<Trade> petList = page.getRecords();
        PageVo pageVo = new PageVo(petList,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getTradeDetail(Long tradeId) {
        Trade trade = getBaseMapper().selectById(tradeId);
        TradeVo tradeVo = BeanCopyUtils.copyOne(trade, TradeVo.class);
        tradeVo.setCreateName(userService.getById(tradeVo.getCreateBy()).getNickName());
        return ResponseResult.okResult(tradeVo);
    }

    @Override
    public ResponseResult addTrade(Trade trade) {
        if (trade.getTradeName()!=null&&trade.getPrice()!=null&&trade.getCounts()!=null){
            save(trade);
            return ResponseResult.okResult();
        }throw new SystemException(AppHttpCodeEnum.ADD_ERROR);
    }

    @Override
    public ResponseResult tradeList(Integer pageNum, Integer pageSize, Trade trade) {
        LambdaQueryWrapper<Trade> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Trade::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.orderByDesc(Trade::getIsTop);
        System.out.println("管理员id为"+SecurityUtils.getUserId());
        queryWrapper.eq(SecurityUtils.getUserId()!=1,Trade::getCreateBy,SecurityUtils.getUserId());
        queryWrapper.eq(Objects.nonNull(trade.getTradeName()),Trade::getTradeName,trade.getTradeName());
        //分页查询
        Page<Trade> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        List<Trade> tradeList = page.getRecords();
        PageVo pageVo = new PageVo(tradeList,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
}

